package com.cgly.medical.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.cgly.medical.param.CompanyPolicyParam;
import com.cgly.medical.service.CompanyPolicyService;
import com.cgly.medical.util.Msg;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.RolesAllowed;

/**
 * @author 黄浩然
 */

//@Api(tags = "医药公司政策控制器类")
@RestController
@RequestMapping("/api/company_policys")
@CrossOrigin
public class CompanyPolicyController {

    @Autowired
    private CompanyPolicyService companyPolicyService;

    //查询
    @PostMapping(value = "/{current}/{limit}")
    public Msg getPolicyWithPage(@PathVariable("current") int pn, @PathVariable("limit") int size,
                                 @RequestBody(required = false) CompanyPolicyParam companyPolicyParam) {

        companyPolicyParam.setPn(pn);
        companyPolicyParam.setSize(size);
        Msg msg = companyPolicyService.getAllPolicyWithPage(companyPolicyParam);
        return msg;
    }

    //添加
    //    @RolesAllowed({"1"})
    @PostMapping(value = "")
    public Msg saveCompanyPolicy(@RequestBody CompanyPolicyParam companyPolicyParam) {
        Msg msg = companyPolicyService.saveCompanyPolicy(companyPolicyParam);
        return msg;
    }

    //按id查询
    @GetMapping(value = "/{id}")
    public Msg getCompanyPolicyById(@PathVariable("id") Long id) {
        Msg msg = companyPolicyService.getCompanyPolicyById(id);
        return msg;
    }

    //修改
    //    @RolesAllowed({"1"})
    @PostMapping(value = "/{id}")
    public Msg updateCompanyPolicy(@PathVariable("id") Long id, @RequestBody CompanyPolicyParam companypolicy) {
        if (!StringUtils.hasLength(companypolicy.getTitle())) {
            return Msg.fail().mess("标题不能为空");
        }
        if (!StringUtils.hasLength(companypolicy.getMessage())) {
            return Msg.fail().mess("内容不能为空");
        }
        if (companypolicy.getCompanyId() == null) {
            return Msg.fail().mess("公司ID不能为空");
        }
        return companyPolicyService.updateCompanyPolicy(id, companypolicy);
    }

    //删除
    //    @RolesAllowed({"1"})
    @DeleteMapping("/{id}")
    public Msg deleteCompanyPolicy(@PathVariable("id") Long id) {

        return companyPolicyService.deleteCompanyPolicy(id);
    }
}
